<?php

class Mnews extends CI_Model {

    private $selectByCreatedById = "SELECT t_ntf_news.news_id,t_ntf_news.territory_id,t_mtr_territory.territory_name,t_ntf_news.news_header,t_ntf_news.news_content,t_ntf_news.created_by,t_ntf_news.created_on,t_ntf_news.updated_by,t_ntf_news.updated_on from t_ntf_news INNER JOIN t_mtr_territory ON t_ntf_news.territory_id = t_mtr_territory.territory_id WHERE t_ntf_news.created_by=?";
    private $selectById = "SELECT * from t_ntf_news WHERE news_id=?";
    private $insert = "INSERT INTO t_ntf_news(territory_id,news_header,news_content,created_by,created_on,updated_by,updated_on) VALUES(?,?,?,?,now(),null,null)";
    private $update = "UPDATE t_ntf_news SET territory_id = ?, news_header=?, news_content = ?, updated_by = ?, updated_on = now() WHERE news_id = ?";

    function Ext_Magents() {
        parent::__construct();
    }

    function selectByCreatedById($userId) {
        $query = $this->db->query($this->selectByCreatedById, array($userId));
        return $query->result();
    }

    function selectById($id) {
        $query = $this->db->query($this->selectById, array($id));
        return $query->result();
    }

    function update($territory_id, $news_header, $news_content, $updated_by, $id) {
        $query = $this->db->query($this->update, array($territory_id, $news_header, $news_content, $updated_by, $id));
    }

    function insert($territory_id, $news_header, $news_content, $created_by) {
        $query = $this->db->query($this->insert, array($territory_id, $news_header, $news_content, $created_by));
    }

    function getTerritoryTreeForNews($data = null, $id = 0, $p = 0) {
//Donnie        
//Avoiding recursive lookup by generating sql by depth

        $selectSQL = "SELECT t1.territory_id AS lev1, t2.territory_id as lev2, t3.territory_id as lev3 " .
                "FROM t_mtr_territory AS t1 " .
                "LEFT JOIN t_mtr_territory AS t2 ON t2.parent_id = t1.territory_id " .
                "LEFT JOIN t_mtr_territory AS t3 ON t3.parent_id = t2.territory_id " .
                "WHERE t1.parent_id = 0 ORDER BY lev1,lev2,lev3";
        //echo $selectSQL . "<br/>";
        $selectById = "SELECT territory_id as id, parent_id FROM t_mtr_territory WHERE territory_id = ?";
        $list = $this->db->query($selectSQL, array());
        $list = $list->result();
        $data = array();

        for ($i = 0; $i < sizeof($list); $i++) {
            //echo $i . "<br/>";
            $level1 = $list[$i]->lev1;
            $level2 = $list[$i]->lev2;
            $level3 = $list[$i]->lev3;
            //echo "CHECKING $level1 $level2 $level3<br/>";
            $selectById = "SELECT territory_id as id, parent_id, territory_name as text, user_id, territory_type_id, istatus,ordering FROM t_mtr_territory WHERE territory_id = ? AND territory_type_id IN (1,2,3)";
            $singleElement = $this->db->query($selectById, array($level1));
            $singleLev1 = $singleElement->result();


            if (empty($data[$level1])) {
                $data[$level1] = $singleLev1[0];
            }
            if (!empty($level2)) {
                $singleElement = $this->db->query($selectById, array($level2));
                $singleLev2 = $singleElement->result();
                if (empty($data[$level1]->children[$level2])) {
                    $data[$level1]->children[$level2] = $singleLev2[0];
                }
            }
//            if (!empty($level3)) {
//                $singleElement = $this->db->query($selectById, array($level3));
//                $singleLev3 = $singleElement->result();
//                if (empty($data[$level1]->children[$level2]->children[$level3])) {
//                    $data[$level1]->children[$level2]->children[$level3] = $singleLev3[0];
//                }
//            }
        }
        //Normalize form
        $arrayLevel1 = array();
        $arrayLevel2 = array();
        $arrayLevel3 = array();

        foreach ($data as $id1 => $level1) {

            $arrayLevel2 = array();
            $arrayLevel3 = array();
//            array_push($arrayLevel1, $level1);
            if (isset($level1->children)) {

                // echo "Children level 1 detected<br/>";
                foreach ($level1->children as $id2 => $level2) {

                    if (isset($level2->children)) {
                        foreach ($level2->children as $id3 => $level3) {
							// $arrayLevel3 = new $arrayLevel3() {
                                array_push($arrayLevel3, $level3);
							// }
                        }
//                        $arrayLevel3 = trim_array($arrayLevel3, 1);

                        $level2->children = array();
                        //array_push($level2->children, $arrayLevel3);
                        $level2->children = $arrayLevel3;
                    }
                    array_push($arrayLevel2, $level2);
                }
                $level1->children = array();
                //array_push($level1->children, $arrayLevel2);
                $level1->children = $arrayLevel2;
            }
            array_push($arrayLevel1, $level1);
        }


        // echo "<pre>";
        // print_r($arrayLevel1);
        // echo "</pre>";
//
//        echo "<pre>";
//        // print_r($data);
//        echo "</pre>";
        return json_encode($arrayLevel1);
        // return sizeof($list);
    }

    function trim_array(array $array, $int) {
        $newArray = array();
        for ($i = 0; $i < $int; $i++) {
            array_push($newArray, $array[$i]);
        }
        return (array) $newArray;
    }

    function cleanArray($array) {
        foreach ($array as $index => $value) {
            if (is_array($array[$index]))
                $array[$index] = cleanArray($array[$index]);
            if (empty($value))
                unset($array[$index]);
        }
        return $array;
    }

}
